package org.example.common.interceptor;

import cn.hutool.json.JSONObject;
import org.example.common.JWTHS256;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

//拦截器，用户拦截请求中的token信息进行验证
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        // 拦截处理代码
        if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
            return true;
        }

        String token = request.getHeader("authorization");
        System.out.println("拦截请求头，验证用户...");
        System.out.println(token);
        //验证token，如果通过，返回true通过；否则，返回false拦截
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = null ;
        if(token==null){
            System.out.println("token为空");
            try{
                JSONObject res = new JSONObject();
                res.put("status","400");
                res.put("msg","请求头携带token为空");
                out = response.getWriter();
                out.append(res.toString());
                return false;
            }
            catch (Exception e) {
                e.printStackTrace();
                response.sendError(500);
                return false;
            }
        }

        //解密token
        String account = JWTHS256.vaildToken(token);
        if("-1".equals(account)){
            System.out.println("Token 无效");
            try{
                JSONObject res = new JSONObject();
                res.put("status","400");
                res.put("msg","Token 无效");
                out = response.getWriter();
                out.append(res.toString());
                return false;
            }
            catch (Exception e) {
                e.printStackTrace();
                response.sendError(500);
                return false;
            }
        }else if("-2".equals(account)){
            System.out.println("Token 已过期");
            try{
                JSONObject res = new JSONObject();
                res.put("status","400");
                res.put("msg","Token 已过期");
                out = response.getWriter();
                out.append(res.toString());
                return false;
            }
            catch (Exception e) {
                e.printStackTrace();
                response.sendError(500);
                return false;
            }
        }else if("-3".equals(account)){
            System.out.println("账号为空");
            try{
                JSONObject res = new JSONObject();
                res.put("status","400");
                res.put("msg","账号为空");
                out = response.getWriter();
                out.append(res.toString());
                return false;
            }
            catch (Exception e) {
                e.printStackTrace();
                response.sendError(500);
                return false;
            }
        }else{
            System.out.println("验证成功!"+account);
            return true;
        }
    }
}
